<?php
core::init();
class core{
    /**
     * 框架加载
     */
    public static function init()
    {
        session_start();
        @self::loadClass();/*加载类文件*/
        define("ENABLE_STATIC",getProperty("enableStatic"));
        date_default_timezone_set(getProperty("timezone"));/*加载时区*/
        @self::loadDomains();/*加载域名绑定*/
        if(session_get("skip-sleep")!="yes"&&cookie_get("skip-sleep")!="yes"){
            usleep(USLEEP_TIME);
        }
        if(args_get("skip-sleep")=="yes"||args_post("skip-sleep")=="yes"){
            session_set("skip-sleep","yes");
            cookie_set("skip-sleep","yes");
        }elseif(args_get("skip-sleep")=="no"||args_post("skip-sleep")=="no"){
            session_delete("skip-sleep");
            cookie_delete("skip-sleep");
        }
    }

    /**
     * 加载类文件
     */
    public static function loadClass()
    {
        /*扫描类文件*/
        if(!is_dir("src")){
            $a = scandir("phar://core.phar/core");
        }else{
            $a = scandir("src/core");
        }
        foreach ($a as $item) {
            if ($item!="."&&$item!=".."&&$item!="core.php") {
                if(!is_dir("src")){
                    include("phar://core.phar/core/$item");
                }else{
                    include("src/core/".$item);
                }
            }
        }
    }

    /**
     * 加载域名绑定
     */
    public static function loadDomains()
    {
        $p=getPath();
        $c=getBindName();
        $enableDomains=getProperty("enableDomains");
        define("HTML_PATH",STATIC_PATH.$c."/html/");
        define("CSS_PATH",STATIC_PATH.$c."/css/");
        define("JS_PATH",STATIC_PATH.$c."/js/");
        define("FILES_PATH",STATIC_PATH.$c."/files/");
        define("IMG_PATH",STATIC_PATH.$c."/img/");
        define("PHP_PATH",CORE_PATH.$c."/");
        define("LOG_PATH",GLOBAL_LOG_PATH.$c."/");
        define("DATA_PATH",GLOBAL_DATA_PATH.$c."/");
        writeLog("Info","加载域名绑定");
        if(($enableDomains==1&&getProperty("enableSSL")==1)||getBindSSL()){
            if(!$_SERVER["HTTPS"]){
                writeLog("Info","从HTTP提升到HTTPS");
                header("Location:https://".$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"]);
            }
        }
        if($c){
            $rm=$_SERVER["REQUEST_METHOD"];
            $rm=strtoupper($rm);
            writeLog("Info","请求方式 = $rm");
            if($rm=="GET"){
                if(file_get_contents(CORE_PATH.$c."/get.php")){
                    if($p=="file"){
                        $type=args_get("type");
                        $name=args_get("name");
                        switch($type){
                            case "js":
                                header('Content-Type:applcation/javascript');
                                echo file_get_contents(JS_PATH.$name.".js");
                                break;
                            case "css":
                                header('Content-Type:text/css;charset=utf-8');
                                echo file_get_contents(CSS_PATH.$name.".css");
                                break;
                            case "jpg":
                            case "jpeg":
                                header('Content-Type:image/jpeg;charset=utf-8');
                                echo file_get_contents(IMG_PATH.$name.".jpg");
                                break;
                            case "svg":
                                header('Content-Type:text/xml;charset=utf-8');
                                echo file_get_contents(IMG_PATH.$name.".svg");
                                break;
                            case "woff2":
                            case "woff":
                            case "ttf":
                            case "eot":
                                header('Content-Type:application/octet-stream;charset=utf-8');
                                writeLog("Info","Read file ".FILES_PATH.$name);
                                echo file_get_contents(FILES_PATH.$name);
                                break;
                            default:
                                echo "文件格式暂不支持";
                        }
                    }else{
                        $aa =CORE_PATH.$c."/get.php";
                        include($aa);
                        if(class_exists("get")){
                            writeLog("Info","访问页面");
                            $d = @call_user_func("get::$p");
                            if (!$d) {
                                loadErrorPage("无法访问", "该页面或地址不存在，请您检查路径后再访问。");
                            }
                        } else {
                            loadErrorPage("无法访问", "该站点($c)的get.php存在问题，因此您无法访问该页面。");
                        }
                    }
                }else{
                    loadErrorPage("无法访问","该站点($c)的get.php不存在或为空，因此您无法访问该页面。");
                }
            }elseif($rm=="POST"){
                if(file_get_contents(CORE_PATH.$c."/post.php")){
                    $aa=CORE_PATH.$c."/post.php";
                    include($aa);
                    if (class_exists("post")){
                        $d=@call_user_func("post::$p");
                        if(!$d){echo "404 Not Found!";};
                        writeLog("Error","找不到页面");
                    }
                }else{
                    writeLog("Error","post.php不存在或为空");
                    echo "该站点($c)的post.php不存在或为空。";
                }
            }else{
                writeLog("Error","不支持的请求方式");
                echo "不支持的请求方式！";
            }
        }else{
            writeLog("Error","域名没有被绑定，当前访问域名为：".$_SERVER["HTTP_HOST"]);
            loadErrorPage("无法访问","该域名或地址被没有绑定，因此您无法访问当前页面。");
        }
    }
}